import React from 'react'
import {Switch,Route,withRouter,Redirect} from 'react-router-dom'
import Quanxian from './Quanxian'

import {inject,observer} from 'mobx-react'
@inject('loginStore')
@observer
class PrivateRoute extends React.Component {
    util=(routerList)=>{
        return routerList.map((route,i)=>{
            if(route.meta&&route.meta.require){
                if(this.props.loginStore.user.isLogin){
                    const premissicons = this.props.loginStore.user.premissicons
                    const rolse = route.role
                    if(rolse.includes(premissicons)){
                        if(route.children){
                            return <Route key={route.name} path={route.path} render={()=>
                                <route.component key={route.name} {...this.props}>
                                    {this.util(route.children)}
                                    <Route key={route.name} path={route.path} exact render={()=><Redirect to={route.redirect}/>}/>
                                </route.component>
                            }/>
                        }else {
                            return <Route key={route.name} exact path={route.path} component={()=>(<route.component/>)}/>
                        }
                    }else {
                        return <Route key={route.name} exact path={route.path} component={Quanxian}/>
                    }
                }else {

                    return <Redirect key={route.name} to='/login'/>
                }
            }else if(route.redirect){
                return  <Route key={route.name} path={route.path} exact render={()=><Redirect to={route.redirect}/>}/>
            }else {
                return <Route key={route.name} path={route.path} component={route.component}/>
            }
        })
    }
    render() {
        let {routers} = this.props
        return (
            <Switch>
                {/*这里就要动态渲染我们的route*/}
                {this.util(routers)}
            </Switch>
        )
    }
}

export default withRouter(PrivateRoute)